//
//  LXChooesCustomViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2022/1/4.
//  Copyright © 2022 LX_App. All rights reserved.
//

#import "LXChooseCustomViewController.h"
#import "KeHuInfoModel.h"
#import "KeHuListTableViewCell.h"
@interface LXChooseCustomViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSMutableArray *data;
@property (nonatomic) NSInteger pageIndex;
@property (nonatomic) NSString *searchStr;
@property (nonatomic) LXTopSearchView *searchView;
@end

@implementation LXChooseCustomViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"选择客户"];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    
    
    LXTopSearchView *searchView = [[LXTopSearchView alloc] initWithFrame:CGRectMake(0, kViewTopHeight, kScreenWidth, 70)];
    searchView.textField.placeholder = @"请输入客户姓名查询";
    [self.view addSubview:searchView];
    [searchView setClickBtnBlock:^(NSString * _Nonnull text) {
        [self.view endEditing:YES];
        self.searchStr = text;
        [self.tableView.mj_header beginRefreshing];
    }];
    self.searchView = searchView;
    
    self.view.backgroundColor = White_Color;
    
    [self.view addSubview:self.tableView];

    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        self.pageIndex = 1;
        [self loadData];
    }];

    [self.tableView.mj_header beginRefreshing];
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
         self.pageIndex++;
        [self loadData];
    }];
    


}

- (void)loadData{

    // type==3 获取全部客户
    NSMutableDictionary *dic = @{
        @"page":@(self.pageIndex),
        @"rows":@(10),
        @"type":@"3",
        @"groupByCustomer":@YES
    }.mutableCopy;
    
    
    if (!StringIsEmpty(self.searchStr)) {
        [dic setValue:self.searchStr forKey:@"keyWords"];
    }
    
    [MSNetworkManager postRevertUrl:POST_ListWithSign_URL params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
         
        [self endRefreshing];
        
        if ([self.tableView.mj_header isRefreshing]||self.pageIndex==1) {
            [self.data removeAllObjects];
            [self.tableView.mj_footer resetNoMoreData];
        }
        NSInteger totalPage = [resonseObj[@"total"] integerValue];
            
        
        [self.data addObjectsFromArray:[KeHuInfoModel  mj_objectArrayWithKeyValuesArray:resonseObj[@"list"]]];
        
        
        if (self.data.count>=totalPage) {
            self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
        }
        else {
            self.tableView.mj_footer.state = MJRefreshStateIdle;
        }
        
            
        [self.tableView reloadData];
            
        [self isHaveEmptyData];
            
       
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
        [self endRefreshing];
        [self isHaveEmptyData];
    } withShowHud:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger index = 0;
    static NSString *identifier = @"KeHuListTableViewCell1";

    KeHuListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass([KeHuListTableViewCell class]) owner:self options:nil] objectAtIndex:index];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    KeHuInfoModel *model= self.data[indexPath.row];
    cell.allModel = model;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KeHuInfoModel *model= self.data[indexPath.row];

    if (model.isRegister == NO) {
        showAlert(@"该客户尚未注册");
        return;
    }
    
    if (self.ToSendValue) {
        KeHuInfoModel *modle = self.data[indexPath.row];
        self.ToSendValue(modle);
        [self.navigationController popViewControllerAnimated:YES];
    }
    
}

- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, MaxY(self.searchView), ScreenWidth, kScreenHeight - MaxY(self.searchView)) style:UITableViewStylePlain];
        _tableView.backgroundColor = White_Color;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 58;
        _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    }
    return _tableView;
}

-(NSMutableArray *)data{
    if (!_data) {
        _data = [NSMutableArray array];
    }
    return _data;
}
- (void)isHaveEmptyData{
    if (self.data.count == 0) {
        self.tableView.startTip = YES;
        self.tableView.tipView.backgroundColor = self.tableView.backgroundColor;
        self.tableView.tipImage = [UIImage imageNamed:@"No_order_icon"];
        self.tableView.tipView.hidden = NO;
        self.tableView.scrollEnabled = YES;
    }else{
        self.tableView.tipView.hidden = YES;
        self.tableView.scrollEnabled = YES;
    }
}


- (void)endRefreshing{
    if ([self.tableView.mj_header isRefreshing]) {
        [self.tableView.mj_header endRefreshing];
    }
    if ([self.tableView.mj_footer isRefreshing]) {
        [self.tableView.mj_footer endRefreshing];
    }

}

@end
